// ??? ???? ?? ? ?? ??? ????? ????
// ??? ??? ????? ?? ??? Set? ????.
var feeds = new Set();

// ?? ?? ??? jQuery Feeds ????? ???.
// https://github.com/camagu/jquery-feeds/
$('.content').feeds({
  // ?? ??? ???? ?? ?? ??
  feeds: {
    ???: 'http://www.alyaoumia.com/feeds/posts/default?alt=rss',
    ???: 'http://www.alyaoum24.com/comments/feed',
    ??: 'http://rss.egloos.com/blog/kwon37xi',
    ???: 'http://feeds.feedburner.com/xguru?format=xml',
    ???: 'http://feeds.feedburner.com/goodhyun',
    ???: 'http://nolboo.github.io/feed.xml',
    ???: 'http://www.kmshack.kr/rss',
    ???: 'http://rss.egloos.com/blog/minjang',
    ???: 'http://blog.seulgi.kim/feeds/posts/default',
    ???: 'https://kimws.wordpress.com/feed/',
    ???: 'http://feeds.feedburner.com/crazytazo?format=xml',
    ???: 'http://taegon.kim/feed',
    ???: 'http://androidhuman.com/rss',
    ???: 'http://www.gisdeveloper.co.kr/rss',
    ??: 'http://occamsrazr.net/tt/index.xml',
    ???: 'http://hoonsbara.tistory.com/rss',
    ???: 'http://likejazz.com/rss',
    ???: 'http://feeds.feedburner.com/junyoung?format=xml',
    ???: 'http://feeds.feedburner.com/rss_outsider_dev?format=xml',
    ???: 'https://arload.wordpress.com/feed/',
    ???: 'http://emptydream.tistory.com/rss',
    ???: 'http://feeds.feedburner.com/codewiz',
    ???: 'http://hyeonseok.com/rss/',
    ???: 'http://blog.kivol.net/rss',
    ???: 'http://qnibus.com/feed/',
    ???: 'http://blog.rss.naver.com/delmadang.xml',
    ???: 'http://www.flowdas.com/blog/feeds/rss/',
    ???: 'http://www.enshahar.me/feeds/posts/default',
    ???: 'http://mobicon.tistory.com/rss',
    ???: 'http://www.buggymind.com/rss',
    ???: 'http://feeds.feedburner.com/sangwook?format=xml',
    ???: 'http://daddycat.blogspot.com/feeds/posts/default',
    ???: 'http://openlook.org/wp/feed/',
    ???: 'http://feeds.feedburner.com/gogamza?format=xml',
    ???: 'http://bcho.tistory.com/rss',
    ???: 'http://rss.egloos.com/blog/aeternum',
    ???: 'http://javacan.tistory.com/rss',
    ???: 'http://feeds.feedburner.com/Smartmob',
    ???: 'http://kkamagui.tistory.com/rss',
    ???: 'https://joone.wordpress.com/feed/',
    ???: 'http://blog.dahlia.kr/rss',
    ???: 'http://blog.fupfin.com/?feed=rss2',
    ??: 'http://rss.egloos.com/blog/recipes',
    ???????: 'http://feeds.feedburner.com/gamedevforever?format=xml',
    NHN: 'http://helloworld.naver.com/rss',
    SSM: 'http://blog.secmem.org/rss',
    ??: 'http://bigmatch.i-um.net/feed/',
    ?????: 'https://blogs.idincu.com/dev/feed/',
    SK???: 'http://feeds.feedburner.com/skpreadme?format=xml',
    VCNC: 'http://engineering.vcnc.co.kr/atom.xml',
    Google: 'http://feeds.feedburner.com/GoogleDevelopersKorea?format=xml',
    Mozilla: 'http://hacks.mozilla.or.kr/feed/'
  },
  
  // ? ?? ???? ?? ???.
  preprocess: function ( feed ) {

    // ??? ????.
    // ??? feed? ???? ???? ????.
    feeds.add(feed);

    // ??? ???? ???? ????
    var rssParam = getUrlParameter('feed');
    
    // feed ????? ???? ??? ?? ??.
    if(rssParam == undefined) {
      
      // 14? ??? ??
      var today = new Date();
      var pubDate = new Date(this.publishedDate);
      var during = today.getTime() - pubDate.getTime();
      if(during > 14 * 24 * 60 * 60 * 1000) { // ???????
        return false;
      }
    }
    // feed ????? ???? ?? ???? ??.
    else {
      // display only one source
      if(rssParam !== feed.feedUrl) {
        return false;
      }
    }
  },
  // ? ?? ??? ???? ???? ??
  entryTemplate: function(entry) {
    
    var template = 
        '<div class="entry">' +
          '<div><span><!=feedTitle!></span></div>' +
          '<div><span><!=publishedDate!></span></div>' +
          '<div><a href="<!=link!>" target="_blank"><h3><!=title!></h3></a></div>' +
          '<div><p><!=contentSnippet!></p></div>' +
        '</div>';
    
    return this.tmpl(template, entry);
  },
  // ?? ??? ? ??? ????.
  onComplete: function(entries) {

    // Set? ????? ??? ????.
    // Set??? ??? ???? ?? ? ??. ?? ???? ???.
    feeds.forEach(function(item){
      var menuDom = '<core-item><div><a href="?feed=' + item.feedUrl + '">'+ item.title + '</a></div></core-item>';
      $('#menu').append(menuDom);
    });    
  }
});

// URL ????? ?? ???.
// http://stackoverflow.com/questions/19491336/get-url-parameter-jquery
function getUrlParameter(sParam)
{
  var sPageURL = window.location.search.substring(1);
  var sURLVariables = sPageURL.split('&');
  for (var i = 0; i < sURLVariables.length; i++)
  {
    var sParameterName = sURLVariables[i].split('=');
    if (sParameterName[0] == sParam)
    {
      return sParameterName[1];
    }
  }
}